﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using JLL.China.Application;
using JLL.China.Infrastructure.Services;
using JLL.China.Web.Areas.Portal.Models;
using JLL.China.Web.Libraries;

namespace JLL.China.Web.Areas.Portal.Controllers
{
    public class GlobalBannerController : Controller
    {
        protected int defaultPageSize = Int32.Parse(WebConfigurationManager.AppSettings["page_size_default"]);
        // GET: /Portal/HomeBanner/
        public GlobalBannerController()
        {
           ViewData["now_controller"] = "Portal/GlobalBanner/";
           ViewData["TitleEvent"] = "Global Banner";
           ViewData["Module"] = "Global";
        }
   
        public ActionResult Index(decimal? id)
        {
            //            ViewData["bannercount"] = Session["bannercount"];
            if (!checkUserLogin())
            {
                return RedirectToAction("LogIn", "Home", new { area = "" });
            }
           
                BannerModel model = new BannerModel();
                List<FileInfo> files = FromFolder("~/Media/Banner/Global");
                ViewData["files"] = files;
                model.Banner = BannerService.FindByID_LanguageFid(null, 1);
                model.Banner_Local = BannerService.FindByID_LanguageFid(null, 2);
                return View(model);
          
        }

        public ActionResult DeleteFile(string filename)
        {
            System.IO.File.Delete(filename);
            //            Session["message"] = "Delete file successfully.";
            //            Session["messageType"] = "Success";
            ShowMessage("Delete file successfully.", "Success");
            return RedirectToAction("Index");
        }

        List<FileInfo> FromFolder(string folderName)
        {
            string path = Server.MapPath( folderName);
            //string SourecPath = HttpContext.Request.MapPath(path);
            if (!Directory.Exists(path))
            {
                return null;
            }
            DirectoryInfo sourcedinfo = new DirectoryInfo(path);

            var files = sourcedinfo.GetFiles();
            List<FileInfo> fs = new List<FileInfo>();
            foreach (var fileInfo in files)
            {
                //                if (fileInfo.Name.StartsWith(sufix))
                //                {
                fs.Add(fileInfo);
                //                }

            }
            return fs;
        }
        public bool checkUserLogin()
        {
            var loginAcc = this.Session[Constants.Session_User] as Model.Admin;
            if (loginAcc == null)
            {
                return false;
            }
            return true;
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(BannerModel model)
        {
            //            ViewData["bannercount"] = Session["bannercount"];
            if (!checkUserLogin())
            {
                return RedirectToAction("LogIn", "Home", new { area = "" });
            }
            decimal res = 0;//insert/update unsuccessfully
           
            model.Banner.BannerGroupFID = 2;//global banner
            model.Banner.Status = true;
            model.Banner.Type = "static";
            var banner = BannerService.FindByID_LanguageFid(null, 1);
            if (banner == null) //for insert
            {
                res = BannerService.CreateBanner(model.Banner, model.Banner_Local);

            }
            else //for update
            {
                model.Banner.Banner_ID = banner.Banner_ID;
                res = BannerService.UpdateBanner(model.Banner, model.Banner_Local);
            }
            if (res != -1)
            {

                ShowMessage("Saved successfully.", "Success");
            }
            else
            {
                ShowMessage("Saved unsuccessfully.", "Error");
            }

            return RedirectToAction("Index",new {id=res});

        }
        public ActionResult UploadFiles(HttpPostedFileBase FileData, FormCollection forms)
        {
            try
            {
                if (FileData == null)
                {
                    return null;
                }

                string mediaRootPath = Server.MapPath("~/Media/Banner/Global");



                if (!Directory.Exists(mediaRootPath))
                {
                    Directory.CreateDirectory(mediaRootPath);
                }
                string name = System.IO.Path.GetFileName(FileData.FileName);
                string fullFilePath = mediaRootPath + "\\" + name;
                FileData.SaveAs(fullFilePath);

                

            }
            catch (Exception)
            {

                
            }
            return RedirectToAction("Index");

        }
        void ShowMessage(string msg, string type)
        {
            Session["message"] = msg;
            Session["messageType"] = type;
        }

    }
}
